package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.BabyWeight;
import cn.b.training.baby_education_training.repository.BabyWeightRepository;
import cn.b.training.baby_education_training.service.BabyInfoService;
import cn.b.training.baby_education_training.service.BabyWeightService;
import cn.b.training.baby_education_training.service.dto.BabyWeightDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-01 09:20
 **/
@Service
public class BabyWeightServiceImpl implements BabyWeightService {
    @Autowired
    private BabyWeightRepository babyWeightRepository;
    @Autowired
    private BabyInfoService babyInfoService;
    @Override
    public BabyWeight insertWeight(String recordTime, Float weight) throws ParseException {
        BabyWeight babyWeight = new BabyWeight();
        babyWeight.setWeight(weight);
        Date now = new Date();
        Long nowTime = now.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date record = sdf.parse(recordTime);
        Long oldTime = record.getTime();
        long day = (nowTime - oldTime)/(24*60*60*1000);
        long month;
        long year;
        month = day / 30;
        if (month<1){
            babyWeight.setAge(day+"天");
        }else if (month>=1&&month<12){
            babyWeight.setAge(month+"个月");
        }else {
            year = month / 12;
            month = month % 12;
            babyWeight.setAge(year+"年"+month+"个月");
        }
        babyWeight.setRecordTime(record);
        return babyWeight;
    }

    @Override
    public BabyWeight getWeightById(Long id) {
        return babyWeightRepository.findWeightById(id);
    }

    @Override
    public List<BabyWeight> getWeightByBabyId(Long babyId) {
        return babyWeightRepository.findWeightByBabyId(babyId);
    }

    @Override
    public boolean updateWeightInfo(BabyWeightDto weightDto) throws Exception {
        BabyWeight babyWeight = babyWeightRepository.findWeightById(weightDto.getId());
        if (babyWeight==null){
            throw new Exception("未找到体重信息");
        }
        BabyInfo babyInfo = new BabyInfo();
        if (weightDto.getBabyId()!=null){
            babyInfo = babyInfoService.getBabyById(weightDto.getBabyId());
            if (babyInfo==null){
                throw new Exception("未找到婴儿信息");
            }
            babyWeight.setBabyId(weightDto.getBabyId());
        }else {
            babyInfo = babyInfoService.getBabyById(babyWeight.getBabyId());
        }
        babyWeight.setWeight(weightDto.getWeight());
        babyWeight.setAge(weightDto.getAge());
        babyWeight.setRecordTime(Timestamp.valueOf(LocalDateTime.now()));
        babyWeightRepository.save(babyWeight);
        if (babyWeight!=null){
            return true;
        }
        return false;
    }

    @Override
    public Page<BabyWeight> getAllWeights(Integer page, Integer size) {
        return this.babyWeightRepository.findAll(PageRequest.of(page,size));
    }

    @Override
    public boolean deleteWeight(Long id) {
        try{
            this.babyWeightRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
